package com.ruoyi.btlfbreed.service.impl;

import java.util.List;

import com.ruoyi.btlfbreed.domain.MyTbChickenSalesData;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.btlfbreed.domain.TbChickenOutRegistry;
import com.ruoyi.btlfbreed.mapper.TbChickenSalesInfoMapper;
import com.ruoyi.btlfbreed.domain.TbChickenSalesInfo;
import com.ruoyi.btlfbreed.service.ITbChickenSalesInfoService;

/**
 * 肉鸡销售Service业务层处理
 * 
 * @author codeman
 * @date 2024-05-08
 */
@Service
public class TbChickenSalesInfoServiceImpl implements ITbChickenSalesInfoService 
{
    @Autowired
    private TbChickenSalesInfoMapper tbChickenSalesInfoMapper;

    /**
     * 查询肉鸡销售
     * 
     * @param saleId 肉鸡销售主键
     * @return 肉鸡销售
     */
    @Override
    public TbChickenSalesInfo selectTbChickenSalesInfoBySaleId(Long saleId)
    {
        return tbChickenSalesInfoMapper.selectTbChickenSalesInfoBySaleId(saleId);
    }

    /**
     * 查询肉鸡销售列表
     * 
     * @param tbChickenSalesInfo 肉鸡销售
     * @return 肉鸡销售
     */
    @Override
    public List<TbChickenSalesInfo> selectTbChickenSalesInfoList(TbChickenSalesInfo tbChickenSalesInfo)
    {
        return tbChickenSalesInfoMapper.selectTbChickenSalesInfoList(tbChickenSalesInfo);
    }

    /**
     * 新增肉鸡销售
     * 
     * @param tbChickenSalesInfo 肉鸡销售
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbChickenSalesInfo(TbChickenSalesInfo tbChickenSalesInfo)
    {
        tbChickenSalesInfo.setCreateTime(DateUtils.getNowDate());
        int rows = tbChickenSalesInfoMapper.insertTbChickenSalesInfo(tbChickenSalesInfo);
        insertTbChickenOutRegistry(tbChickenSalesInfo);
        return rows;
    }

    /**
     * 修改肉鸡销售
     * 
     * @param tbChickenSalesInfo 肉鸡销售
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbChickenSalesInfo(TbChickenSalesInfo tbChickenSalesInfo)
    {
        tbChickenSalesInfo.setUpdateTime(DateUtils.getNowDate());
        tbChickenSalesInfoMapper.deleteTbChickenOutRegistryBySaleId(tbChickenSalesInfo.getSaleId());
        insertTbChickenOutRegistry(tbChickenSalesInfo);
        return tbChickenSalesInfoMapper.updateTbChickenSalesInfo(tbChickenSalesInfo);
    }

    /**
     * 批量删除肉鸡销售
     * 
     * @param saleIds 需要删除的肉鸡销售主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbChickenSalesInfoBySaleIds(Long[] saleIds)
    {
        tbChickenSalesInfoMapper.deleteTbChickenOutRegistryBySaleIds(saleIds);
        return tbChickenSalesInfoMapper.deleteTbChickenSalesInfoBySaleIds(saleIds);
    }

    /**
     * 删除肉鸡销售信息
     * 
     * @param saleId 肉鸡销售主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbChickenSalesInfoBySaleId(Long saleId)
    {
        tbChickenSalesInfoMapper.deleteTbChickenOutRegistryBySaleId(saleId);
        return tbChickenSalesInfoMapper.deleteTbChickenSalesInfoBySaleId(saleId);
    }

    @Override
    public List<MyTbChickenSalesData> getMonthSalesData(String year, String month)
    {
        return tbChickenSalesInfoMapper.getMonthSalesData(year,month);
    }

    /**
     * 新增肉鸡出栏信息
     * 
     * @param tbChickenSalesInfo 肉鸡销售对象
     */
    public void insertTbChickenOutRegistry(TbChickenSalesInfo tbChickenSalesInfo)
    {
        List<TbChickenOutRegistry> tbChickenOutRegistryList = tbChickenSalesInfo.getTbChickenOutRegistryList();
        Long saleId = tbChickenSalesInfo.getSaleId();
        Long customerId = tbChickenSalesInfo.getCustomerId();
        if (StringUtils.isNotNull(tbChickenOutRegistryList))
        {
            List<TbChickenOutRegistry> list = new ArrayList<TbChickenOutRegistry>();
            for (TbChickenOutRegistry tbChickenOutRegistry : tbChickenOutRegistryList)
            {
                tbChickenOutRegistry.setSaleId(saleId);
                tbChickenOutRegistry.setCustomerId(customerId);
                list.add(tbChickenOutRegistry);
            }
            if (list.size() > 0)
            {
                tbChickenSalesInfoMapper.batchTbChickenOutRegistry(list);
            }
        }
    }
}
